/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view.statistik;

import databasehandling.HandlerFactory;
import databasehandling.StatistikHandler;
import java.awt.Color;
import java.awt.Graphics2D;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.scene.chart.PieChart;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import utility.DateCombo;
import utility.StatistikModul;
import view.MainGUI;

/**
 *
 * @author Jeppe
 */
public class StatistikPanel extends javax.swing.JPanel {

    private final MainGUI mainGUI;

    /**
     * Creates new form StatistikPanel
     *
     * @param mainGUI
     */
    public StatistikPanel(MainGUI mainGUI) {
        initComponents();

        this.mainGUI = mainGUI;
        //<editor-fold defaultstate="collapsed" desc="SetCombos">
        fraMånedCombo.removeAllItems();
        fraÅrCombo.removeAllItems();
        tilMånedCombo.removeAllItems();
        tilÅrCombo.removeAllItems();
        for (int i = 0; i < DateCombo.hentMåneder().length; i++) {
            fraMånedCombo.addItem(DateCombo.hentMåneder()[i]);
        }
        for (int i = 0; i < DateCombo.hentÅr().length; i++) {
            fraÅrCombo.addItem(DateCombo.hentÅr()[i]);
        }
        for (int i = 0; i < DateCombo.hentMåneder().length; i++) {
            tilMånedCombo.addItem(DateCombo.hentMåneder()[i]);
        }
        for (int i = 0; i < DateCombo.hentÅr().length; i++) {
            tilÅrCombo.addItem(DateCombo.hentÅr()[i]);
        }

//</editor-fold>
    }

    /**
     * Vis statistik generere en eller flere diagrammer alt efter hvad brugeren
     * vil have genereret statistik over.
     *
     * @param accelerator Knappen-som-kalder-metoden's navn. Enten idag, sidste
     * uge, sidste måned. Eller brugerdef.
     */
    private void visStatistik(String accelerator) {
        statistikPanel.removeAll();
        int antalBesøgende = 0;
        int antalBørn = 0;
        int antalVoksne = 0;
        int antalGratister = 0;

        Calendar cal = Calendar.getInstance();

        try {
            if (accelerator.equals("Idag")) {
                idagKnap.setBackground(MainGUI.BUTTON_SELECTED_COLOR);
                antalVoksne = HandlerFactory.getInstance().getStatistikHandler().getAntalVoksneVedDato(cal);
                antalGratister = HandlerFactory.getInstance().getStatistikHandler().getAntalGratisterVedDato(cal);
                antalBørn = HandlerFactory.getInstance().getStatistikHandler().getAntalBørnVedDato(cal);
                idagKnap.setBackground(MainGUI.BUTTON_SELECTED_COLOR);
            } else if (accelerator.equals("Uge")) {
                for (int i = 0; i < 7; i++) {
                    if (cal.get(Calendar.DAY_OF_WEEK) != 1) {
                        cal.add(Calendar.DAY_OF_MONTH, -1);
                        System.out.println("Der bliver trukket en fra dato.");
                    }
                }
                for (int i = 0; i < 8; i++) {
                    cal.add(Calendar.DAY_OF_MONTH, 1);
                    System.out.println("Der bliver lagt én til datoen. D." + cal.get(Calendar.DAY_OF_MONTH) + ".");
                    antalVoksne = antalVoksne + HandlerFactory.getInstance().getStatistikHandler().getAntalVoksneVedDato(Calendar.getInstance());
                    antalGratister = HandlerFactory.getInstance().getStatistikHandler().getAntalGratisterVedDato(Calendar.getInstance());
                    antalBørn = HandlerFactory.getInstance().getStatistikHandler().getAntalBørnVedDato(Calendar.getInstance());
                }
            }

        } catch (SQLException ex) {
            Logger.getLogger(StatistikPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        //<editor-fold defaultstate="collapsed" desc="LavPanelOg sæt det ind">
        ObservableList<PieChart.Data> pieChartData = null;
        pieChartData = FXCollections.observableArrayList(
                new PieChart.Data("Voksne", antalVoksne),
                new PieChart.Data("Gratister", antalGratister),
                new PieChart.Data("Børn", antalBørn)
        );
        JFXPanel jfx = StatistikModul.getJfxPanel(pieChartData);
        statistikPanel.add(jfx);
        jfx.setOpaque(false);
        jfx.setSize(500, 500);
        jfx.setVisible(true);
        statistikPanel.revalidate();
        statistikPanel.repaint();
//</editor-fold>
    }

    private void setLabels() {
        ugeKnap.setText("Uge " + Calendar.getInstance().get(Calendar.WEEK_OF_YEAR));
        månedKnap.setText("Måned: " + DateCombo.hentMåneder()[Calendar.getInstance().getTime().getMonth()]);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        idagKnap = new javax.swing.JButton();
        ugeKnap = new javax.swing.JButton();
        månedKnap = new javax.swing.JButton();
        brugerDefknapPanel = new javax.swing.JPanel();
        fraMånedCombo = new javax.swing.JComboBox();
        fraÅrCombo = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tilMånedCombo = new javax.swing.JComboBox();
        tilÅrCombo = new javax.swing.JComboBox();
        brugerDefKnap = new javax.swing.JButton();
        statistikPanel = new javax.swing.JPanel();

        setOpaque(false);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        setLayout(null);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setOpaque(false);
        jPanel2.setLayout(null);

        idagKnap.setText("Idag");
        idagKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                idagKnapActionPerformed(evt);
            }
        });
        jPanel2.add(idagKnap);
        idagKnap.setBounds(0, 0, 200, 100);

        ugeKnap.setText("1 Uge");
        ugeKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ugeKnapActionPerformed(evt);
            }
        });
        jPanel2.add(ugeKnap);
        ugeKnap.setBounds(200, 0, 200, 100);

        månedKnap.setText("1 Måned");
        jPanel2.add(månedKnap);
        månedKnap.setBounds(400, 0, 200, 100);

        brugerDefknapPanel.setLayout(null);

        brugerDefknapPanel.add(fraMånedCombo);
        fraMånedCombo.setBounds(32, 10, 80, 30);

        brugerDefknapPanel.add(fraÅrCombo);
        fraÅrCombo.setBounds(120, 10, 70, 30);

        jLabel1.setText("Fra:");
        brugerDefknapPanel.add(jLabel1);
        jLabel1.setBounds(10, 20, 30, 14);

        jLabel2.setText("Til:");
        brugerDefknapPanel.add(jLabel2);
        jLabel2.setBounds(10, 58, 20, 14);

        brugerDefknapPanel.add(tilMånedCombo);
        tilMånedCombo.setBounds(32, 50, 80, 30);

        brugerDefknapPanel.add(tilÅrCombo);
        tilÅrCombo.setBounds(120, 50, 70, 30);

        brugerDefKnap.setFocusable(false);
        brugerDefknapPanel.add(brugerDefKnap);
        brugerDefKnap.setBounds(0, 0, 200, 100);

        jPanel2.add(brugerDefknapPanel);
        brugerDefknapPanel.setBounds(600, 0, 202, 100);

        add(jPanel2);
        jPanel2.setBounds(0, 0, 810, 100);

        statistikPanel.setOpaque(false);
        statistikPanel.setLayout(null);
        add(statistikPanel);
        statistikPanel.setBounds(0, 100, 800, 510);
    }// </editor-fold>//GEN-END:initComponents

    private void idagKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_idagKnapActionPerformed
        visStatistik("Idag");
        idagKnap.setBackground(MainGUI.BUTTON_SELECTED_COLOR);
    }//GEN-LAST:event_idagKnapActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        setLabels();
    }//GEN-LAST:event_formComponentShown

    private void ugeKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ugeKnapActionPerformed
        visStatistik("Uge");
    }//GEN-LAST:event_ugeKnapActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton brugerDefKnap;
    private javax.swing.JPanel brugerDefknapPanel;
    private javax.swing.JComboBox fraMånedCombo;
    private javax.swing.JComboBox fraÅrCombo;
    private javax.swing.JButton idagKnap;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton månedKnap;
    private javax.swing.JPanel statistikPanel;
    private javax.swing.JComboBox tilMånedCombo;
    private javax.swing.JComboBox tilÅrCombo;
    private javax.swing.JButton ugeKnap;
    // End of variables declaration//GEN-END:variables
}
